package me.zhengjie.modules.finace_taxation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.modules.finace_taxation.domain.FtSearchVo;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.finace_taxation.domain.FtServiceType;
import me.zhengjie.modules.finace_taxation.service.FtServiceTypeService;
import me.zhengjie.modules.finace_taxation.service.dto.FtServiceTypeDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtServiceTypeQueryParam;
import me.zhengjie.modules.finace_taxation.service.mapper.FtServiceTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author 杨荣森
* @date 2021-07-01
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FtServiceTypeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FtServiceTypeServiceImpl implements FtServiceTypeService {

    // private final RedisUtils redisUtils;
    private final FtServiceTypeMapper ftServiceTypeMapper;

    @Override
    public PageInfo<FtServiceTypeDto> queryAll(FtServiceTypeQueryParam query, Pageable pageable) {
        QueryWrapper<FtServiceType> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FtServiceType::getName, query.getName());
        IPage<FtServiceType> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FtServiceType> page = ftServiceTypeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FtServiceTypeDto.class);
    }

    @Override
    public List<FtServiceTypeDto> queryAll(FtServiceTypeQueryParam query){
        return ConvertUtil.convertList(ftServiceTypeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FtServiceTypeDto.class);
    }

    @Override
    public FtServiceType getById(String id) {
        return ftServiceTypeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FtServiceTypeDto findById(String id) {
        return ConvertUtil.convert(getById(id), FtServiceTypeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FtServiceTypeDto resources) {
        FtServiceType entity = ConvertUtil.convert(resources, FtServiceType.class);
        return ftServiceTypeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FtServiceTypeDto resources){
        FtServiceType entity = ConvertUtil.convert(resources, FtServiceType.class);
        int ret = ftServiceTypeMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return ftServiceTypeMapper.deleteBatchIds(ids);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    @Override
    public List<FtSearchVo> searchByKeyWords(Map<String, Object> map) {
        List<FtSearchVo> searchVoList = ftServiceTypeMapper.searchByKeyWords(map);
        return searchVoList;
    }


    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    @Override
    public void download(List<FtServiceTypeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FtServiceTypeDto ftServiceType : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("名称", ftServiceType.getName());
              map.put("图标", ftServiceType.getIcon());
              map.put("标签", ftServiceType.getLabel());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }
}
